package com.sluggard.security.core.config;


import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.sluggard.security.properties.SecurityConfigProperties;
import jakarta.annotation.Resource;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Locale;
import java.util.Properties;

@Configuration
public class SecurityBeanConfig {

    @Resource
    private SecurityConfigProperties securityConfigProperties;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 验证码
     *
     * @return
     */
    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        Properties properties = new Properties();
        properties.setProperty(Constants.KAPTCHA_BORDER, securityConfigProperties.getKaptcha().getBorder());
        properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, securityConfigProperties.getKaptcha().getBorderColor());
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, securityConfigProperties.getKaptcha().getFontColor());
        properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, securityConfigProperties.getKaptcha().getWidth().toString());
        properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, securityConfigProperties.getKaptcha().getHeight().toString());
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, securityConfigProperties.getKaptcha().getFontSize().toString());
        properties.setProperty(Constants.KAPTCHA_SESSION_KEY, securityConfigProperties.getKaptcha().getSessionKey());
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, securityConfigProperties.getKaptcha().getCharLength().toString());
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, securityConfigProperties.getKaptcha().getFontNames());
        if (securityConfigProperties.getKaptcha().isNoNoise()) {
            properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
        }

        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(new Config(properties));
        return defaultKaptcha;
    }

    @Bean
    public MessageSource messageSource() {
        Locale.setDefault(Locale.CHINA);
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.addBasenames("classpath:org/springframework/security/messages_zh_CN");
        return messageSource;
    }



//    @Bean
//    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
//    @DependsOn(value = "projectingArgumentResolverBeanPostProcessor")
//    Advisor preAuthorize(MyAuthorizationManagerBeforeMethodInterceptor manager) {
//        return AuthorizationManagerBeforeMethodInterceptor.preAuthorize(manager);
//    }

    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("123456"));
    }
}
